package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Summerday
 */
public class BuildStream {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3);
        // 通过集合的stream()方法创建顺序流
        Stream<Integer> stream1 = list.stream();

        // 通过集合的parallelStream()方法创建顺序流
        Stream<Integer> parallelStream1 = list.parallelStream();

        // 通过parallel()将顺序流转化为并行流
        Stream<Integer> parallelStream2 = list.stream().parallel();

        int[] arr = {1, 2, 3};
        // 通过数组创建流
        IntStream stream2 = Arrays.stream(arr);

        // 通过Stream的静态方法创建流
        Stream<Integer> stream3 = Stream.of(1, 2, 3);
        Stream<Integer> stream4 = Stream.iterate(1, x -> x + 1).limit(3);
        Stream<Integer> stream5 = Stream.generate(()-> new Random().nextInt()).limit(3);

        stream1.forEach(x -> System.out.print(x + " "));
        System.out.println();

        stream2.forEach(x -> System.out.print(x + " "));
        System.out.println();

        stream3.forEach(x -> System.out.print(x + " "));
        System.out.println();

        stream4.forEach(x -> System.out.print(x + " "));
        System.out.println();

        stream5.forEach(x -> System.out.print(x + " "));
        System.out.println();

        parallelStream1.forEach(x -> System.out.print(x + " "));
        System.out.println();

        parallelStream2.forEach(x -> System.out.print(x + " "));
        System.out.println();
    }
}
